@extends('admin.layout')
@section('content')
<div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                用户管理
                <small>用户列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>用户管理</a></li>
                <li><a href="#">用户列表</a></li>
              
            </ol>
        </section>
 		<section class="content">
            <div class="row">
                <div class="col-xs-12">
					<div class="box">
                        <div class="box-header">
                            <h3 class="box-title">快速查看用户</h3>
                        </div><!-- /.box-header -->
							
                        <div class="box-body">
                          @if(session('info'))
                            <div class="alert alert-success alert-dismissable">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                                <h4><i class="icon fa fa-warning"></i> 警告!</h4>
                                 <ul>
                                    {{ session('info') }}
                                 </ul>
                                </div>                              
                            @endif  
							<form action="{{ url('/admin/user/index') }}" method="get">
							<div class="row">
								<div class="col-md-4">
									  <div class="form-group">
                                        <select name="num" class="form-control">
                                        <option 
										@if(!empty($request['num']) &&
										$request['num'] == 10)
											selected="selected"
										@endif 
                                        value="10">10</option>
                                        <option 

											@if(!empty($request['num']) &&
											$request['num'] == 25)
											selected="selected"
											@endif 

                                        value="25">25</option>
                                        <option 

											@if(!empty($request['num']) &&
											$request['num'] == 50)
											selected="selected"
											@endif 

                                        value="50">50</option>
                                        <option 

											@if(!empty($request['num']) &&
											$request['num'] == 100)
											selected="selected"
											@endif 

                                        value="100">100</option>
                                        </select>
                                		</div>
								</div>
								<div class="col-md-4 col-md-offset-4">
									<div class="input-group input-group">
                                		<input type="text" name="keywords" value="{{ $request['keywords'] or ''}}" class="form-control">
                   					 	<span class="input-group-btn">
                   			   			<button class="btn btn-info btn-flat">搜索</button>
                   					 </span>
                           			 </div><!-- /input-group -->    
								</div>
							</div> <br/> 
							</form>
                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>状态</th>
                                    <th>注册时间</th>
                                    <th>最后登录时间</th>
                                    <th>发布数量</th>                                    
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $user)                               
                                <tr>
                                    <td class="ids">{{ $user -> id }}</td>
                                    <td class="username">{{ $user -> username }}</td>
                                    <td>{{ $user -> email }}</td>
                                    <td class="status">
										@if($user -> status ==0)
										启用
										@else
										禁用
										@endif	
                                    </td>
                                    <td class="username">{{ date('20y-m-d H:i:s',$user -> ftime) }}</td>
                                    <td class="username">{{ date('20y-m-d H:i:s',$user -> ltime) }}</td>
                                    <td class="username">{{ $user -> title_num }}</td>
                                    <td><a href="{{ url('/admin/user/edit') }}/{{ $user -> id }}">编辑</a> | <a href="{{ url('/admin/user/delete') }}/{{ $user -> id }}">删除</a>| <a href="{{ url('/admin/user/details') }}/{{ $user -> id }}">详情</a></td>
                                </tr>                               
                                @endforeach

                            </table>
                            <div class="container">
  							  @foreach ($data as $users)
     							
 						      @endforeach
							</div>

							{!! $data->appends($request)->links() !!}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
  </div><!-- /.content-wrapper -->
  <!-- 状态启用/禁用ajax -->
<script type="text/javascript">

window.onload = function()
{

	$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }


		});

//发送ajax
//添加修改状态
	$(".status").on('click', function(){
			//准备data数据
			var id = $(this).parent().find(".ids").html();
			var t = $(this);
			// 准备ajax
			$.ajax({
				url:"{{ url('/admin/user/ajaxUpdate') }}",
				type:"POST",
				data:{id:id},
				success:function(data){
					if(data == 0)
					{
						$(t).html("启用");
					}else if(data == 1)
					{
						$(t).html("禁用");
					}else
					{
						alert('修改失败');
					}
					},
					dataType:'json'
			});

	});


		function dblClick()
		{
					//获取当前对象
				var t = $(this);
				//获取id 和 username
				var id = $(this).parent().find(".ids").html();
				var oldName = $(this).html();

				// 设置input框
				var inpt = $("<input type='text'/>");
				inpt.val(oldName);
				$(this).html(inpt);

				//获取焦点
				inpt.select();
				//失去焦点事件
				inpt.on('blur', function(){
					//获取新值
					var newName = inpt.val();

					//发送ajax
					$.ajax({
						url:"{{ url('/admin/user/ajaxName') }}",
						type:"POST",
						data:{id:id, username:newName},
						success:function(data){
								if(data == 0)
								{
									t.html(newName);
								}else if(data == 1)
								{
									t.html(oldName);
								}else
								{
									alert('用户已存在');
								}
						},
						dataType:'json'
					});
					//添加双击事件
					t.on('dblclick', dblClick);

				});
				//清双击事件
				$(this).unbind('dblclick');
		}

	//修改用户名
	$(".username").on('dblclick',dblClick);

}
</script>
@endsection